# -*- coding:utf-8 -*-
#! python3

import win32gui
import win32api
from win32con import *
import math
import time
from facecat import *
import timer
import random
try:
    import thread
except ImportError:
    import _thread as thread

#更新悬浮状态
#views:视图集合
def updateView(views):
	updateViewDefault(views)

#绘制视图
#view:视图
#paint:绘图对象
#clipRect:区域
def onPaint(view, paint, clipRect):
	onPaintDefault(view, paint, clipRect)

#绘制视图边线
#view:视图
#paint:绘图对象
#clipRect:区域
def onPaintBorder(view, paint, clipRect):
	onPaintBorderDefault(view, paint, clipRect)

#视图的鼠标移动方法
#view 视图
#mp 坐标
#buttons 按钮 0未按下 1左键 2右键
#clicks 点击次数
#delta 滚轮值
def onMouseMove(view, mp, buttons, clicks, delta):
	onMouseMoveDefault(view, mp, buttons, clicks, delta)
		
#视图的鼠标按下方法
#view 视图
#mp 坐标
#buttons 按钮 0未按下 1左键 2右键
#clicks 点击次数
#delta 滚轮值
def onMouseDown(view, mp, buttons, clicks, delta):
	onMouseDownDefault(view, mp, buttons, clicks, delta)

#视图的鼠标抬起方法
#view 视图
#mp 坐标
#buttons 按钮 0未按下 1左键 2右键
#clicks 点击次数
#delta 滚轮值
def onMouseUp(view, mp, buttons, clicks, delta):
	onMouseUpDefault(view, mp, buttons, clicks, delta)

#视图的鼠标点击方法
#view 视图
#firstTouch:是否第一次触摸 
#firstPoint:第一次触摸的坐标 
#secondTouch:是否第二次触摸 
#secondPoint:第二次触摸的坐标
#clicks 点击次数
def onClick(view, firstTouch, firstPoint, secondTouch, secondPoint, clicks):
	onClickDefault(view, firstTouch, firstPoint, secondTouch, secondPoint, clicks)

#视图的鼠标滚动方法
#view 视图
#mp 坐标
#buttons 按钮 0未按下 1左键 2右键
#clicks 点击次数
#delta 滚轮值
def onMouseWheel(view, mp, buttons, clicks, delta):
	onMouseWheelDefault(view, mp, buttons, clicks, delta)

#重新计算最大最小值
def onCalculateChartMaxMin(chart):
	chart.candleMax = 0
	chart.candleMin = 0
	chart.volMax = 0
	chart.volMin = 0
	chart.indMin = 0
	chart.indMin = 0
	if chart.data != None and len(chart.data) > 0:
		lastValidIndex = chart.lastVisibleIndex
		if chart.lastValidIndex != -1:
			lastValidIndex = chart.lastValidIndex
		for i in range(chart.firstVisibleIndex, lastValidIndex + 1):
			if i == chart.firstVisibleIndex:
				chart.candleMax = chart.data[i].data1
				chart.candleMin = chart.data[i].data1
			else:
				if chart.candleMax < chart.data[i].data1:
					chart.candleMax = chart.data[i].data1
				if chart.candleMin > chart.data[i].data1:
					chart.candleMin = chart.data[i].data1
				if chart.candleMax < chart.data[i].data2:
					chart.candleMax = chart.data[i].data2
				if chart.candleMin > chart.data[i].data2:
					chart.candleMin = chart.data[i].data2
				if chart.candleMax < chart.data[i].data3:
					chart.candleMax = chart.data[i].data3
				if chart.candleMin > chart.data[i].data3:
					chart.candleMin = chart.data[i].data3
				if chart.candleMax < chart.data[i].data4:
					chart.candleMax = chart.data[i].data4
				if chart.candleMin > chart.data[i].data4:
					chart.candleMin = chart.data[i].data4
				if chart.candleMax < chart.data[i].data5:
					chart.candleMax = chart.data[i].data5
				if chart.candleMin > chart.data[i].data5:
					chart.candleMin = chart.data[i].data5
				if chart.candleMax < chart.data[i].data6:
					chart.candleMax = chart.data[i].data6
				if chart.candleMin > chart.data[i].data6:
					chart.candleMin = chart.data[i].data6


#绘制线条
#chart:K线
#paint:绘图对象
#clipRect:裁剪区域
#divIndex:图层
#datas:数据
#color:颜色
#selected:是否选中
def drawChartLines2(chart, paint, clipRect, divIndex, datas, color, text):
	drawPoints = []
	lastX = 0
	lastY = 0
	showLast = False
	for i in range(0, len(datas)):
		x = getChartX(chart, i + chart.firstVisibleIndex)
		value = datas[i]
		y = getChartY(chart, divIndex, value)
		drawPoints.append(FCPoint(x, y))
		if i + chart.firstVisibleIndex == len(chart.data) - 1:
			showLast = True
		lastX = x
		lastY = y
	paint.drawPolyline(color, 2, 0, drawPoints)
	if showLast:
		r = 10
		paint.fillEllipse(color, lastX - r, lastY - r, lastX + r, lastY + r)
		if chart.paint.defaultUIStyle == "dark":
			paint.drawEllipse("rgb(255,255,255)", 1, 0, lastX - r, lastY - r, lastX + r, lastY + r)
		elif chart.paint.defaultUIStyle == "light":
			paint.drawEllipse("rgb(0,0,0)", 1, 0, lastX - r, lastY - r, lastX + r, lastY + r)
		font = "Default,14"
		tSize = paint.textSize(text, font)
		if chart.paint.defaultUIStyle == "dark":
			paint.drawText(text, "rgb(255,255,255)", font, lastX - tSize.cx / 2, lastY + tSize.cy + r)
		elif chart.paint.defaultUIStyle == "light":
			paint.drawText(text, "rgb(0,0,0)", font, lastX - tSize.cx / 2, lastY + tSize.cy + r)

#绘制K线
#chart:K线
#paint:绘图对象
#clipRect:裁剪区域
def onPaintChartStock(chart, paint, clipRect):
	global drawColors
	if chart.data != None and len(chart.data) > 0:
		lastValidIndex = chart.lastVisibleIndex
		if chart.lastValidIndex != -1:
			lastValidIndex = chart.lastValidIndex
		datas1 = []
		for i in range(chart.firstVisibleIndex, lastValidIndex + 1):
			datas1.append(chart.data[i].data1)
		drawChartLines2(chart, paint, clipRect, 0, datas1, drawColors[0], "项目1")
            
		datas2 = []
		for i in range(chart.firstVisibleIndex, lastValidIndex + 1):
			datas2.append(chart.data[i].data2)
		drawChartLines2(chart, paint, clipRect, 0, datas2, drawColors[1], "项目2")
            
		datas3 = []
		for i in range(chart.firstVisibleIndex, lastValidIndex + 1):
			datas3.append(chart.data[i].data3)
		drawChartLines2(chart, paint, clipRect, 0, datas3, drawColors[2], "项目3")
            
		datas4 = []
		for i in range(chart.firstVisibleIndex, lastValidIndex + 1):
			datas4.append(chart.data[i].data4)
		drawChartLines2(chart, paint, clipRect, 0, datas4, drawColors[3], "项目4")
            
		datas5 = []
		for i in range(chart.firstVisibleIndex, lastValidIndex + 1):
			datas5.append(chart.data[i].data5)
		drawChartLines2(chart, paint, clipRect, 0, datas5, drawColors[4], "项目5")
            
		datas6 = []
		for i in range(chart.firstVisibleIndex, lastValidIndex + 1):
			datas6.append(chart.data[i].data6)
		drawChartLines2(chart, paint, clipRect, 0, datas6, drawColors[5], "项目6")

#绘制十字线
#chart:K线
#paint:绘图对象
#clipRect:裁剪区域
def onPaintChartCrossLine(chart, paint, clipRect):
	global drawColors
	if chart.data != None and len(chart.data) > 0:
		candleDivHeight = getCandleDivHeight(chart)
		volDivHeight = getVolDivHeight(chart)
		indDivHeight = getIndDivHeight(chart)
		crossLineIndex = chart.crossStopIndex
		if crossLineIndex == -1:
			crossLineIndex = chart.lastVisibleIndex
		drawTitles = []
		drawTitles.append("项目1 " + toFixed(chart.data[crossLineIndex].data1, chart.candleDigit))
		drawTitles.append("项目2 " + toFixed(chart.data[crossLineIndex].data2, chart.candleDigit))
		drawTitles.append("项目3 " + toFixed(chart.data[crossLineIndex].data3, chart.candleDigit))
		drawTitles.append("项目4 " + toFixed(chart.data[crossLineIndex].data4, chart.candleDigit))
		drawTitles.append("项目5 " + toFixed(chart.data[crossLineIndex].data5, chart.candleDigit))
		drawTitles.append("项目6 " + toFixed(chart.data[crossLineIndex].data6, chart.candleDigit))

		iLeft = chart.leftVScaleWidth + 5
		for i in range(0, len(drawTitles)):
			tSize = paint.textSize(drawTitles[i], chart.font)
			paint.drawText(drawTitles[i], drawColors[i], chart.font, iLeft, 5 + tSize.cy / 2)
			iLeft += tSize.cx + 5

appPaint = FCPaint() #创建绘图对象
appPaint.defaultUIStyle = "dark"
#注册事件
appPaint.onPaint = onPaint 
appPaint.onPaintBorder = onPaintBorder
appPaint.onMouseDown = onMouseDown 
appPaint.onMouseMove = onMouseMove 
appPaint.onMouseUp = onMouseUp
appPaint.onMouseWheel = onMouseWheel
appPaint.onClick = onClick
appPaint.onCalculateChartMaxMin = onCalculateChartMaxMin
appPaint.onUpdateView = updateView
appPaint.onPaintChartStock = onPaintChartStock
appPaint.onPaintChartCrossLine = onPaintChartCrossLine

#消息循环
def WndProc(hwnd,msg,wParam,lParam):
	return WndProcDefault(appPaint,hwnd,msg,wParam,lParam)

#创建窗体
createMainWindow(appPaint, "facecat-py", WndProc)

#默认颜色
drawColors = []
drawColors.append("rgb(59,174,218)")
drawColors.append("rgb(185,63,150)")
drawColors.append("rgb(219,68,83)")
drawColors.append("rgb(246,187,67)")
drawColors.append("rgb(216,112,173)")
drawColors.append("rgb(140,192,81)")
drawColors.append("rgb(233,87,62)")
drawColors.append("rgb(150,123,220)")
drawColors.append("rgb(75,137,220)")
drawColors.append("rgb(170,178,189)")

#初始化图形
chart2 = FCChart()
#填充显示
chart2.dock = "fill"
chart2.leftVScaleWidth = 70
chart2.rightVScaleWidth = 0
chart2.vScaleDistance = 60
chart2.hScalePixel = 11
chart2.hScaleHeight = 30
chart2.candleDivPercent = 1
chart2.volDivPercent = 0
chart2.indDivPercent = 0
chart2.rightSpace = 50
chart2.cycle = "tick"
chart2.backColor = "rgb(0,0,0)"
chart2.borderColor = "rgb(100,100,100)"
chart2.textColor = "rgb(255,255,255)"
chart2.scaleColor = "rgb(100,100,100)"
chart2.crossTipColor = "rgb(50,50,50)"
chart2.crossLineColor = "rgb(100,100,100)"
chart2.gridColor = "rgba(100,100,100,125)"
#添加到主框架
addView(chart2, appPaint)

#检查数据
def checkNewData(a='', b=''):
	global appPaint
	global chart2
	lastData = None
	if len(chart2.data) > 1:
		lastData = chart2.data[len(chart2.data) - 1]
	data = SecurityData()
	data.close = 0
	data.date = len(chart2.data)
	data.data1 = random.randint(0,100) - 50
	data.data2 = random.randint(0,100) - 50
	data.data3 = random.randint(0,100) - 50
	data.data4 = random.randint(0,100) - 50
	data.data5 = random.randint(0,100) - 50
	data.data6 = random.randint(0,100) - 50
	if lastData != None:
		data.data1 = data.data1 + lastData.data1
		data.data2 = data.data2 + lastData.data2
		data.data3 = data.data3 + lastData.data3
		data.data4 = data.data4 + lastData.data4
		data.data5 = data.data5 + lastData.data5
		data.data6 = data.data6 + lastData.data6
	chart2.data.append(data)
	resetChartVisibleRecord(chart2)
	checkChartLastVisibleIndex(chart2)
	onCalculateChartMaxMin(chart2)
	invalidate(appPaint)

#启动秒表
timer.set_timer(50, checkNewData)

#获取窗体大小
rect = win32gui.GetClientRect(appPaint.hWnd)
#更新布局
appPaint.size = FCSize(rect[2] - rect[0], rect[3] - rect[1])
updateView(appPaint.views)
showWindow(appPaint)